package cn.shadowsouls.system.manger.impl;

import cn.shadowsouls.common.domain.Result;
import cn.shadowsouls.system.bean.domain.UserLinkVote;
import cn.shadowsouls.system.bean.domain.Vote;
import cn.shadowsouls.system.bean.domain.VoteContent;
import cn.shadowsouls.system.manger.VoteManger;
import cn.shadowsouls.system.service.UserLinkVoteService;
import cn.shadowsouls.system.service.VoteContentService;
import cn.shadowsouls.system.service.VoteService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shadowsouls
 * @version 1.0
 */
@Component
public class VoteMangerImpl implements VoteManger {

    // 投票选项表
    @Autowired
    VoteContentService contentService;

    // 投票表
    @Autowired
    VoteService voteService;

    // 用户管理的投票表
    @Autowired
    UserLinkVoteService linkVoteService;

    @Override
    public boolean createVote(Vote vote, List<String> options, String userId) {

        if (exitVoteTitle(vote.getVoteName()))
            return false;

        voteService.save(vote);
        QueryWrapper<Vote> wrapper = new QueryWrapper<>();
        wrapper
                .eq("vote_name", vote.getVoteName())
                .eq("vote_intro", vote.getVoteIntro());

        Integer voteId = voteService.getOne(wrapper).getVoteId();

        linkVoteService.save(new UserLinkVote(Integer.valueOf(userId), voteId));
        voteAddOption(String.valueOf(voteId), options);

        return true;
    }

    @Override
    public boolean removeVote() {
        return false;
    }

    @Override
    public boolean editVoteOption() {
        return false;
    }

    @Override
    public Result getVoteDetail(Integer id) {

        QueryWrapper<VoteContent> VCWrapper = new QueryWrapper<>();
        QueryWrapper<Vote> voteQueryWrapper = new QueryWrapper<>();

        VCWrapper.eq("vote_id", id);
        voteQueryWrapper.eq("vote_id", id);

        List<VoteContent> voteContents = contentService.list(VCWrapper);
        Vote vote = voteService.getOne(voteQueryWrapper);


        Result result = new Result<Object>();

        result.setCode("200");

        result.setMsg("投票细节");
        result.setData(new VD(vote, voteContents));

        return result;

    }


    @Override
    public Result getAllVote() {

        Result<List<Vote>> result = new Result();
        result.setCode("200");
        result.setMsg("请求所有投票成功");
        result.setAuthorCode("-1");
        result.setData(voteService.list());
        return result;
    }

    @Override
    public Result getOwnVote(String userId) {
        Result<List<Vote>> result = new Result();
        result.setCode("200");
        result.setMsg("请求所有投票成功");
        result.setAuthorCode(userId);

        QueryWrapper<UserLinkVote> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", userId);

        // ULV表的相关内容
        List<UserLinkVote> ULV = linkVoteService.list(wrapper);
        List<Vote> votes = new ArrayList<>();

        // 根据获取的vid来获取投票的内容
        ULV.forEach(userLinkVote -> {
            String vid = String.valueOf(userLinkVote.getVid());

            QueryWrapper<Vote> vwrapper = new QueryWrapper<>();
            vwrapper.eq("vote_id", vid);

            votes.add(voteService.getOne(vwrapper));
        });

        result.setData(votes);

        return result;
    }

    @Override
    public boolean addVoteOptionCount(Integer vcId) {
        QueryWrapper<VoteContent> voteContentQueryWrapper = new QueryWrapper<>();
        voteContentQueryWrapper.eq("vc_id", vcId);
        VoteContent option = contentService.getOne(voteContentQueryWrapper);
        option.setOptionCount(option.getOptionCount() + 1);

        return contentService.updateById(option);
    }

    // 给投票添加选项
    public void voteAddOption(String voteID, List<String> options) {

        List<VoteContent> voteContents = new ArrayList<>();
        options.forEach(name -> {
            voteContents.add(new VoteContent(new Integer(voteID), name));
        });

        contentService.saveBatch(voteContents);

    }

    @Override
    public boolean exitVoteTitle(String name) {

        QueryWrapper<Vote> wrapper = new QueryWrapper<>();
        wrapper.eq("vote_name", name);

        return voteService.count(wrapper) > 0;

    }

    @Data
    @AllArgsConstructor
    class VD {
        Vote vote;
        List<VoteContent> contents;
    }

}
